package com.qh.resourcehub.info.ipresource.dao.impl;

import com.qh.resourcehub.info.entity.ApplicationData;
import com.qh.resourcehub.info.ipresource.dao.IpApplicationDao;
import com.qh.resourcehub.utils.XuguUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.sql.*;
import java.util.*;

@Repository("IpApplicationDao")
public class IpApplicationDaoImpl implements IpApplicationDao {
    @Autowired
    private XuguUtils xuguUtils;

    String[] Ipcolumns = {"ID", "APPLICANT_UNIT"};

    @Override
    public List<Map<String, Object>> queryIpDataService(ApplicationData applicationData) {
        List<Map<String, Object>> result = new ArrayList<>();
        Connection connection = null;
        PreparedStatement pstmtUser = null;

        try {
            connection = xuguUtils.getConnection();
            List<Object> queryParams = new ArrayList<>();

            // 构建查询SQL
            StringBuilder sqlQuery = new StringBuilder("SELECT * FROM APPLICATION_DATA WHERE 1=1 ");

            // 动态条件拼接
            if (!StringUtils.isEmpty(applicationData.getApplyUser())) {
                sqlQuery.append(" AND APPLY_USER = ?");
                queryParams.add(applicationData.getApplyUser());
            }

            // 按时间排序
            sqlQuery.append(" ORDER BY APPLICATION_TIME DESC ");

            // 分页参数
            int page = (applicationData.getPage() != 0) ? applicationData.getPage() : 0;
            int size = (applicationData.getSize() != 0) ? applicationData.getSize() : 10;
            int offset = page * size;
            sqlQuery.append(" LIMIT ? OFFSET ?");
            queryParams.add(size);
            queryParams.add(offset);

            // 创建 PreparedStatement
            pstmtUser = connection.prepareStatement(sqlQuery.toString());

            // 设置查询参数
            for (int i = 0; i < queryParams.size(); i++) {
                pstmtUser.setObject(i + 1, queryParams.get(i));
            }

            // 执行查询
            ResultSet rs = pstmtUser.executeQuery();

            // 获取ResultSet的元数据
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();

            while (rs.next()) {
                Map<String, Object> row = new HashMap<>();

                // 遍历每一列
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnLabel(i); // 获取列名
                    Object columnValue = rs.getObject(i); // 获取列值

                    // 转换列名为驼峰命名
                    String camelCaseColumnName = convertToCamelCase(columnName);
                    row.put(camelCaseColumnName, columnValue);
                }
                result.add(row);
            }

            return result;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pstmtUser != null) {
                    pstmtUser.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return Collections.emptyList();
    }

    /**
     * IP新增
     *
     * @param applicationData
     * @return
     */
    @Override
    public int ApplicationSave(ApplicationData applicationData) {
        int result = 0;
        Connection connection = null;
        PreparedStatement pstmtUser = null;
        try {
            connection = xuguUtils.getConnection();
            // 新增操作
            StringBuilder sqlUser = new StringBuilder("INSERT INTO APPLICATION_DATA (");
            StringBuilder placeholders = new StringBuilder(" VALUES (");
            List<Object> values = new ArrayList<>();

            if (!StringUtils.isEmpty(applicationData.getId() )) {
                sqlUser.append("ID, ");
                placeholders.append("?, ");
                values.add(applicationData.getId());
            }
            if (!StringUtils.isEmpty(applicationData.getProcessStatus())) {
                sqlUser.append("PROCESS_STATUS, ");
                placeholders.append("?, ");
                values.add(applicationData.getProcessStatus());
            }
            if (!StringUtils.isEmpty(applicationData.getApplicantUnit())) {
                sqlUser.append("APPLICANT_UNIT, ");
                placeholders.append("?, ");
                values.add(applicationData.getApplicantUnit());
            }
            if (!StringUtils.isEmpty(applicationData.getApplicantDepartment() )) {
                sqlUser.append("APPLICANT_DEPARTMENT, ");
                placeholders.append("?, ");
                values.add(applicationData.getApplicantDepartment());
            }
            if (!StringUtils.isEmpty(applicationData.getApplicantUser() )) {
                sqlUser.append("APPLICANT_USER, ");
                placeholders.append("?, ");
                values.add(applicationData.getApplicantUser());
            }
            if (!StringUtils.isEmpty(applicationData.getSystemSecurityOfficer() )) {
                sqlUser.append("SYSTEM_SECURITY_OFFICER, ");
                placeholders.append("?, ");
                values.add(applicationData.getSystemSecurityOfficer());
            }
            if (!StringUtils.isEmpty(applicationData.getContactInfo())) {
                sqlUser.append("CONTACT_INFO, ");
                placeholders.append("?, ");
                values.add(applicationData.getContactInfo());
            }
            if (!StringUtils.isEmpty(applicationData.getIdNumber())) {
                sqlUser.append("ID_NUMBER, ");
                placeholders.append("?, ");
                values.add(applicationData.getIdNumber());
            }
            if (!StringUtils.isEmpty(applicationData.getEmail())) {
                sqlUser.append("EMAIL, ");
                placeholders.append("?, ");
                values.add(applicationData.getEmail());
            }
            if (!StringUtils.isEmpty(applicationData.getPersonnelType())) {
                sqlUser.append("PERSONNEL_TYPE, ");
                placeholders.append("?, ");
                values.add(applicationData.getPersonnelType());
            }
            if (applicationData.getApplicationTime() != null) {
                sqlUser.append("APPLICATION_TIME, ");
                placeholders.append("?, ");
                values.add(Timestamp.valueOf(applicationData.getApplicationTime())); // 转换为 Timestamp
            }
            if (!StringUtils.isEmpty(applicationData.getSystemName())) {
                sqlUser.append("SYSTEM_NAME, ");
                placeholders.append("?, ");
                values.add(applicationData.getSystemName());
            }
            if (!StringUtils.isEmpty(applicationData.getSystemAccount())) {
                sqlUser.append("SYSTEM_ACCOUNT, ");
                placeholders.append("?, ");
                values.add(applicationData.getSystemAccount());
            }
            if (!StringUtils.isEmpty(applicationData.getSystemPassword())) {
                sqlUser.append("SYSTEM_PASSWORD, ");
                placeholders.append("?, ");
                values.add(applicationData.getSystemPassword());
            }
            if (!StringUtils.isEmpty(applicationData.getServerStandard())) {
                sqlUser.append("SERVER_STANDARD, ");
                placeholders.append("?, ");
                values.add(applicationData.getServerStandard());
            }
            if (!StringUtils.isEmpty(applicationData.getQuantity())) {
                sqlUser.append("QUANTITY, ");
                placeholders.append("?, ");
                values.add(applicationData.getQuantity());
            }
            if (!StringUtils.isEmpty(applicationData.getStorageCapacity())) {
                sqlUser.append("STORAGE_CAPACITY, ");
                placeholders.append("?, ");
                values.add(applicationData.getStorageCapacity());
            }
            if (!StringUtils.isEmpty(applicationData.getOperatingSystem())) {
                sqlUser.append("OPERATING_SYSTEM, ");
                placeholders.append("?, ");
                values.add(applicationData.getOperatingSystem());
            }
            if (!StringUtils.isEmpty(applicationData.getWifiDeviceType())) {
                sqlUser.append("WIFI_DEVICE_TYPE, ");
                placeholders.append("?, ");
                values.add(applicationData.getWifiDeviceType());
            }
            if (!StringUtils.isEmpty(applicationData.getWifiAccountType())) {
                sqlUser.append("WIFI_ACCOUNT_TYPE, ");
                placeholders.append("?, ");
                values.add(applicationData.getWifiAccountType());
            }
            if (!StringUtils.isEmpty(applicationData.getIpTerminalType())) {
                sqlUser.append("IP_TERMINAL_TYPE, ");
                placeholders.append("?, ");
                values.add(applicationData.getIpTerminalType());
            }
            if (!StringUtils.isEmpty(applicationData.getIpServerType() )) {
                sqlUser.append("IP_SERVER_TYPE, ");
                placeholders.append("?, ");
                values.add(applicationData.getIpServerType());
            }
            if (!StringUtils.isEmpty(applicationData.getMacAddress())) {
                sqlUser.append("MAC_ADDRESS, ");
                placeholders.append("?, ");
                values.add(applicationData.getMacAddress());
            }
            if (!StringUtils.isEmpty(applicationData.getDeploymentArea())) {
                sqlUser.append("DEPLOYMENT_AREA, ");
                placeholders.append("?, ");
                values.add(applicationData.getDeploymentArea());
            }
            if (!StringUtils.isEmpty(applicationData.getReviewer())) {
                sqlUser.append("REVIEWER, ");
                placeholders.append("?, ");
                values.add(applicationData.getReviewer());
            }
            if (applicationData.getReviewComments() != null) {
                sqlUser.append("REVIEW_COMMENTS, ");
                placeholders.append("?, ");
                values.add(applicationData.getReviewComments());
            }
            if (applicationData.getDataType() != null) {
                sqlUser.append("DATA_TYPE, ");
                placeholders.append("?, ");
                values.add(applicationData.getDataType());
            }
            if (applicationData.getReviewTime() != null) {
                sqlUser.append("REVIEW_TIME, ");
                placeholders.append("?, ");
                values.add(applicationData.getReviewTime());
            }
            if (applicationData.getApplicantUnitDepartment() != null) {
                sqlUser.append("APPLICANT_UNITDEPARTMENT, ");
                placeholders.append("?, ");
                values.add(applicationData.getApplicantUnitDepartment());
            }
            if (applicationData.getApplyUser() != null) {
                sqlUser.append("APPLY_USER, ");
                placeholders.append("?, ");
                values.add(applicationData.getApplyUser());
            }
            if (applicationData.getApplyArea() != null) {
                sqlUser.append("APPLIY_AREA, ");
                placeholders.append("?, ");
                values.add(applicationData.getApplyArea());
            }

            if (values.isEmpty()) {
                return result; // 如果没有任何字段，直接返回
            }

            sqlUser.setLength(sqlUser.length() - 2); // 去掉最后的逗号和空格
            placeholders.setLength(placeholders.length() - 2); // 去掉最后的逗号和空格

            sqlUser.append(")").append(placeholders).append(")");

            pstmtUser = connection.prepareStatement(sqlUser.toString());

            // 设置参数
            for (int i = 0; i < values.size(); i++) {
                if (values.get(i) instanceof Integer) {
                    pstmtUser.setInt(i + 1, (Integer) values.get(i));
                } else if (values.get(i) instanceof Timestamp) {
                    pstmtUser.setTimestamp(i + 1, (Timestamp) values.get(i));
                } else if (values.get(i) instanceof Time) {
                pstmtUser.setTime(i + 1, (Time) values.get(i));
                }  else {
                    pstmtUser.setObject(i + 1, values.get(i));
                }
            }

            System.out.println("Generated SQL: " + sqlUser);
            System.out.println("Parameters: " + values);
            result = pstmtUser.executeUpdate();  // 执行新增操作
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 确保资源关闭
            if (pstmtUser != null) {
                try {
                    pstmtUser.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }


    @Override
    public int ApplicationUpdataById(ApplicationData applicationData) {
        int result = 0;
        Connection connection = null;
        PreparedStatement pstmtUser = null;
        try {
            connection = xuguUtils.getConnection();
            // 修改操作
            StringBuilder sqlUser = new StringBuilder("UPDATE APPLICATION_DATA SET ");
            List<Object> values = new ArrayList<>();
            if (!StringUtils.isEmpty(applicationData.getProcessStatus())) {
                sqlUser.append("PROCESS_STATUS = ?, ");
                values.add(applicationData.getProcessStatus());
            }
            if (!StringUtils.isEmpty(applicationData.getApplicantUnit())) {
                sqlUser.append("APPLICANT_UNIT = ?, ");
                values.add(applicationData.getApplicantUnit());
            }
            if (!StringUtils.isEmpty(applicationData.getApplicantDepartment() )) {
                sqlUser.append("APPLICANT_DEPARTMENT = ?, ");
                values.add(applicationData.getApplicantDepartment());
            }
            if (!StringUtils.isEmpty(applicationData.getApplicantUser() )) {
                sqlUser.append("APPLICANT_USER = ?, ");
                values.add(applicationData.getApplicantUser());
            }
            if (!StringUtils.isEmpty(applicationData.getSystemSecurityOfficer() )) {
                sqlUser.append("SYSTEM_SECURITY_OFFICER = ?, ");
                values.add(applicationData.getSystemSecurityOfficer());
            }
            if (!StringUtils.isEmpty(applicationData.getContactInfo())) {
                sqlUser.append("CONTACT_INFO = ?, ");
                values.add(applicationData.getContactInfo());
            }
            if (!StringUtils.isEmpty(applicationData.getIdNumber())) {
                sqlUser.append("ID_NUMBER = ?, ");
                values.add(applicationData.getIdNumber());
            }
            if (!StringUtils.isEmpty(applicationData.getEmail())) {
                sqlUser.append("EMAIL = ?, ");
                values.add(applicationData.getEmail());
            }
            if (!StringUtils.isEmpty(applicationData.getPersonnelType())) {
                sqlUser.append("PERSONNEL_TYPE = ?, ");
                values.add(applicationData.getPersonnelType());
            }
            if (applicationData.getApplicationTime() != null) {
                sqlUser.append("APPLICATION_TIME = ?, ");
                values.add(Timestamp.valueOf(applicationData.getApplicationTime())); // 转换为 Timestamp
            }
            if (!StringUtils.isEmpty(applicationData.getSystemName())) {
                sqlUser.append("SYSTEM_NAME = ?, ");
                values.add(applicationData.getSystemName());
            }
            if (!StringUtils.isEmpty(applicationData.getSystemAccount())) {
                sqlUser.append("SYSTEM_ACCOUNT = ?, ");
                values.add(applicationData.getSystemAccount());
            }
            if (!StringUtils.isEmpty(applicationData.getSystemPassword())) {
                sqlUser.append("SYSTEM_PASSWORD = ?, ");
                values.add(applicationData.getSystemPassword());
            }
            if (!StringUtils.isEmpty(applicationData.getServerStandard())) {
                sqlUser.append("SERVER_STANDARD = ?, ");
                values.add(applicationData.getServerStandard());
            }
            if (!StringUtils.isEmpty(applicationData.getQuantity())) {
                sqlUser.append("QUANTITY = ?, ");
                values.add(applicationData.getQuantity());
            }
            if (!StringUtils.isEmpty(applicationData.getStorageCapacity())) {
                sqlUser.append("STORAGE_CAPACITY = ?, ");
                values.add(applicationData.getStorageCapacity());
            }
            if (!StringUtils.isEmpty(applicationData.getOperatingSystem())) {
                sqlUser.append("OPERATING_SYSTEM = ?, ");
                values.add(applicationData.getOperatingSystem());
            }
            if (!StringUtils.isEmpty(applicationData.getWifiDeviceType())) {
                sqlUser.append("WIFI_DEVICE_TYPE = ?, ");
                values.add(applicationData.getWifiDeviceType());
            }
            if (!StringUtils.isEmpty(applicationData.getWifiAccountType())) {
                sqlUser.append("WIFI_ACCOUNT_TYPE = ?, ");
                values.add(applicationData.getWifiAccountType());
            }
            if (!StringUtils.isEmpty(applicationData.getIpTerminalType())) {
                sqlUser.append("IP_TERMINAL_TYPE = ?, ");
                values.add(applicationData.getIpTerminalType());
            }
            if (!StringUtils.isEmpty(applicationData.getIpServerType() )) {
                sqlUser.append("IP_SERVER_TYPE = ?, ");
                values.add(applicationData.getIpServerType());
            }
            if (!StringUtils.isEmpty(applicationData.getMacAddress())) {
                sqlUser.append("MAC_ADDRESS = ?, ");
                values.add(applicationData.getMacAddress());
            }
            if (!StringUtils.isEmpty(applicationData.getDeploymentArea())) {
                sqlUser.append("DEPLOYMENT_AREA = ?, ");
                values.add(applicationData.getDeploymentArea());
            }
            if (!StringUtils.isEmpty(applicationData.getReviewer())) {
                sqlUser.append("REVIEWER = ?, ");
                values.add(applicationData.getReviewer());
            }
            if (applicationData.getReviewComments() != null) {
                sqlUser.append("REVIEW_COMMENTS = ?, ");
                values.add(applicationData.getReviewComments());
            }
            if (applicationData.getDataType() != null) {
                sqlUser.append("DATA_TYPE = ?, ");
                values.add(applicationData.getDataType());
            }
            if (applicationData.getReviewTime() != null) {
                sqlUser.append("REVIEW_TIME = ?, ");
                values.add(applicationData.getReviewTime());
            }
            if (applicationData.getApplicantUnitDepartment() != null) {
                sqlUser.append("APPLICANT_UNITDEPARTMENT = ?, ");
                values.add(applicationData.getApplicantUnitDepartment());
            }
            if (applicationData.getApplyArea() != null) {
                sqlUser.append("APPLIY_AREA = ?, ");
                values.add(applicationData.getApplyArea());
            }

            if (values.isEmpty()) {
                return result; // 如果没有任何字段，直接返回
            }

            // 去掉最后的逗号和空格
            if (values.size() > 0) {
                sqlUser.setLength(sqlUser.length() - 2);  // 去掉最后一个多余的 ", "
            } else {
                return result; // 如果没有需要更新的字段，直接返回
            }

            // 拼接 WHERE 子句并添加 ID 参数
            sqlUser.append(" WHERE ID = ?");
            values.add(applicationData.getId());

            // 打印 SQL 和参数，调试用
            System.out.println("SQL: " + sqlUser.toString());
            System.out.println("Values: " + values);

            // 创建 PreparedStatement
            pstmtUser = connection.prepareStatement(sqlUser.toString());

            // 设置参数
            for (int i = 0; i < values.size(); i++) {
                pstmtUser.setObject(i + 1, values.get(i));
            }

            result = pstmtUser.executeUpdate();
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 确保资源关闭
            if (pstmtUser != null) {
                try {
                    pstmtUser.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    @Override
    public int ApplicationDelById(ApplicationData applicationData) {
        int result = 0;
        Connection connection = null;
        PreparedStatement pstmtUser = null;
        try {
            connection = xuguUtils.getConnection();
            // 根据 ID 删除记录的 SQL 语句
            String sql = "DELETE FROM APPLICATION_DATA WHERE ID = ?";
            pstmtUser = connection.prepareStatement(sql);
            // 设置参数：这里假设 applicationData.getId() 返回的是 ID 值
            pstmtUser.setString(1, applicationData.getId());  // 假设 ID 是 Long 类型，依据实际情况设置参数类型
            // 执行删除操作
            result = pstmtUser.executeUpdate();
            return result;  // 返回影响的行数，若删除成功，返回的值会大于 0
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 确保资源关闭
            if (pstmtUser != null) {
                try {
                    pstmtUser.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;  // 如果发生异常或没有删除任何记录，返回 0
    }

    @Override
    public int IPApplicationSubmit(ApplicationData applicationData) {
        int result = 0;
        Connection connection = null;
        PreparedStatement pstmtUser = null;
        try {
            connection = xuguUtils.getConnection();
            StringBuilder sqlUser = new StringBuilder("UPDATE APPLICATION_DATA SET ");
            List<Object> values = new ArrayList<>();
            // 修改操作
            if ("00".equals(applicationData.getDataType())){
                applicationData.setDataType("01");
                if (applicationData.getUserCode() != null) {
                    sqlUser.append("REVIEWER = ?, ");
                    values.add(applicationData.getUserCode());
                }
            } else if ("01".equals(applicationData.getDataType())) {
                applicationData.setDataType("02");
                if (applicationData.getUserCode() != null) {
                    sqlUser.append("REVIEWER_INFO = ?, ");
                    values.add(applicationData.getUserCode());
                }
                //添加通过意见
                if (applicationData.getReviewData() != null) {
                    sqlUser.append("REVIEW_COMMENTS = ?, ");
                    values.add(applicationData.getReviewData());
                }
            } else if ("02".equals(applicationData.getDataType())) {
                applicationData.setDataType("03");
                if (applicationData.getUserCode() != null) {
                    sqlUser.append("REVIEWER_GW = ?, ");
                    values.add(applicationData.getUserCode());
                }
                //添加通过意见
                if (applicationData.getReviewData() != null) {
                    sqlUser.append("REVIEWER_GW_COMMENTS = ?, ");
                    values.add(applicationData.getReviewData());
                }
            }else if ("03".equals(applicationData.getDataType())) {
                applicationData.setDataType("04");
                if (applicationData.getUserCode() != null) {
                    sqlUser.append("REVIEWER_SH = ?, ");
                    values.add(applicationData.getUserCode());
                }
                //添加通过意见
                if (applicationData.getReviewData() != null) {
                    sqlUser.append("REVIEWER_SH_COMMENTS = ?, ");
                    values.add(applicationData.getReviewData());
                }
            }
            if (applicationData.getDataType() != null) {
                sqlUser.append("DATA_TYPE = ?, ");
                values.add(applicationData.getDataType());
            }
            if (values.isEmpty()) {
                return result; // 如果没有任何字段，直接返回
            }
            sqlUser.setLength(sqlUser.length() - 2); // 去掉最后的逗号和空格
            sqlUser.append(" WHERE ID = ?");  // 设置 WHERE 条件
            values.add(applicationData.getId());  // 添加 ID 条件
            pstmtUser = connection.prepareStatement(sqlUser.toString());
            // 设置参数
            for (int i = 0; i < values.size(); i++) {
                pstmtUser.setObject(i + 1, values.get(i));
            }
            result = pstmtUser.executeUpdate();  // 执行更新操作
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 确保资源关闭
            if (pstmtUser != null) {
                try {
                    pstmtUser.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    @Override
    public int IPApplicationWithdraw(ApplicationData applicationData) {
        int result = 0;
        Connection connection = null;
        PreparedStatement pstmtUser = null;
        try {
            connection = xuguUtils.getConnection();
            // 修改操作
            StringBuilder sqlUser = new StringBuilder("UPDATE APPLICATION_DATA SET ");
            List<Object> values = new ArrayList<>();
            if (applicationData.getDataType() != null) {
                sqlUser.append("DATA_TYPE = ?, ");
                values.add(applicationData.getDataType());
            }
            if (applicationData.getReviewer() != null) {
                sqlUser.append("REVIEWER = ?, ");
                values.add(applicationData.getReviewer());
            }
            if (values.isEmpty()) {
                return result; // 如果没有任何字段，直接返回
            }
            sqlUser.setLength(sqlUser.length() - 2); // 去掉最后的逗号和空格
            sqlUser.append(" WHERE ID = ?");  // 设置 WHERE 条件
            values.add(applicationData.getId());  // 添加 ID 条件
            pstmtUser = connection.prepareStatement(sqlUser.toString());
            // 设置参数
            for (int i = 0; i < values.size(); i++) {
                if (values.get(i) instanceof Integer) {
                    pstmtUser.setInt(i + 1, (Integer) values.get(i));
                } else if (values.get(i) instanceof Timestamp) {
                    pstmtUser.setTimestamp(i + 1, (Timestamp) values.get(i));
                } else if (values.get(i) instanceof Time) {
                    pstmtUser.setTime(i + 1, (Time) values.get(i));
                }  else {
                    pstmtUser.setObject(i + 1, values.get(i));
                }
            }
            result = pstmtUser.executeUpdate();  // 执行更新操作
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 确保资源关闭
            if (pstmtUser != null) {
                try {
                    pstmtUser.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }
    /**
     * 将下划线命名的列名转换为驼峰命名
     */
    private String convertToCamelCase(String columnName) {
        StringBuilder camelCaseName = new StringBuilder();
        boolean toUpperCase = false;

        for (int i = 0; i < columnName.length(); i++) {
            char c = columnName.charAt(i);

            if (c == '_') {
                toUpperCase = true;  // 下一个字母大写
            } else {
                if (toUpperCase) {
                    camelCaseName.append(Character.toUpperCase(c)); // 转为大写
                    toUpperCase = false;
                } else {
                    camelCaseName.append(Character.toLowerCase(c)); // 转为小写
                }
            }
        }
        return camelCaseName.toString();
    }
}
